<?php
# Name: database.class.php
# File Description: MySQL Class to allow easy and clean access to mysql
# Author: matt austin
# Web: http://www.m.austin.com
# Update: 11/10/2008
# Version: 1.00a

/*

        include("inc/Database.class.php");

	$dbh = new Database('server', 'username', 'password', 'database');
	
	// or better with 
        $dbh = new Database(array(
                'server' => 'server',
                'user'   => 'username',
                'password' => 'password',
                'database' => 'database'
        ));	

        $query = $dbh->query('select * from users');
        print_r($dbh->fetch_all($query));

        $user = 'admin';
        $query2 = $dbh->query("SELECT * FROM users WHERE username='%s'", $user);
        print_r($dbh->fetch_row($query2));

*/

	class Database {
		var $server   = ""; //db server
		var $user     = ""; //db user name
		var $password = ""; //db password
		var $database = ""; //db name

		function Database($params){
			// we want args to be a hash, but we will take an array as well
			if(gettype($params) == 'string'){
				$arg_list = array('server','user', 'password', 'database');	
				// get all the params onto the function
				$args = func_get_args();
				$params = array_combine($arg_list,  $args);
			}
			$this->server = $params['server'];
			$this->user = $params['user'];
			$this->password = $params['password'];
			$this->database = $params['database'];
			$this->connect();
		}
	
		function connect(){
			$this->link_id=@mysql_pconnect($this->server,$this->user,$this->password) OR die(mysql_error());

			if (!$this->link_id) {//open failed
				die("Could not connect to server: $this->server.");
			}

			if(!@mysql_select_db($this->database, $this->link_id)) {//no database
				die("Could not open database: $this->database.");
			}

			// unset the data so it can't be dumped
			$this->server='';
			$this->user='';
			$this->password='';
			$this->database='';
			return $this->link_id;
		}
	
		function query($sql){
			// get all args passed in
			$arg_list = func_get_args();
			// the first element is the query
			$sql = array_shift($arg_list);
			// apply mysql_real_escape_string to every argument
			foreach ($arg_list as &$value) {
				$value = $this->escape($value);
			}
			// apply sprintf with array
			$query = vsprintf($sql,$arg_list);
			
			// do the query
			$this->query_id = mysql_query($query) OR die(mysql_error());
			$this->affected_rows = @mysql_affected_rows();
			return $this->query_id;

		}

		function fetch_all($query_id=-1) {
			// retrieve row
			if ($query_id!=-1) {
				$this->query_id=$query_id;
			}
			//loop what we get back
			$rows = array();
			while ($row = mysql_fetch_assoc($this->query_id)) {
				$rows[] = $row;
			}
			//free the mem
			mysql_free_result($this->query_id);
			return $rows;					
		}		
		function fetch_row($query_id=-1) {
			// retrieve row
			if ($query_id!=-1) {
				$this->query_id=$query_id;
			}
			$row = mysql_fetch_assoc($this->query_id);
			mysql_free_result($this->query_id);
			return $row;					
		}
		
		function close(){
			mysql_close($this->link_id);
  		}//ends the close function


		function escape($string) {
			if(get_magic_quotes_gpc()) $string = stripslashes($string);
			return mysql_real_escape_string($string);
		}
	}
?>
